//
//  MessageViewController.m
//  FPTest
//
//  Created by zhaohong on 2017/11/9.
//  Copyright © 2017年 zhaohong. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageModel.h"
@interface MessageViewController ()

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"消息";
    
    
    //将jsonString转换为字典，此时一般是我们请求到服务器返回的json字符串后做的第一件事
    //要求服务器返回的数据类型全为string类型，ios的类型转换对int，boolean较弱
    NSString *jsonString = @"{\"name\":\"JSON中国\",\"url\":\"http://www.json.org.cn\",\"page\":\"88\",\"isNonProfit\":\"true\",\"address\":{\"street\":\"浙大路38号.\",\"city\":\"浙江杭州\",\"country\":\"中国\"},\"links\":[{\"name\":\"Google\",\"url\":\"http://www.google.com\"},{\"name\":\"Baidu\",\"url\":\"http://www.baidu.com\"},{\"name\":\"SoSo\",\"url\":\"http://www.SoSo.com\"}]}";
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    NSLog(@"%@",dic);
    
//    从打印出来的结果看，可以知道最外层有6个键值对，其中address和links较为特殊
//    address可看做一个单独的对象，里面包含有三个键值对
//    links是一个数组，每个数组里面可以看做是一个单独的对象，每个对象中包含两个键值对
//    根据我提到到每个单独数据模块都可以视为一个对象的话，因此我们此数据类型可以划分成三个数据model
//        1.最外层包含了全部数据的MessageModel
//        2.addressModel
//        3.linksModel
//    {
//        address =     {
//            city = "\U6d59\U6c5f\U676d\U5dde";
//            country = "\U4e2d\U56fd";
//            street = "\U6d59\U5927\U8def38\U53f7.";
//        };
//        isNonProfit = true;
//        links =     (
//                     {
//                         name = Google;
//                         url = "http://www.google.com";
//                     },
//                     {
//                         name = Baidu;
//                         url = "http://www.baidu.com";
//                     },
//                     {
//                         name = SoSo;
//                         url = "http://www.SoSo.com";
//                     }
//                     );
//        name = "JSON\U4e2d\U56fd";
//        page = 88;
//        url = "http://www.json.org.cn";
//    }
    
    //建立三个model对象,并且对应每个对象的内容都创建属性
    //将字典转为model数据以及调用方法如下
    MessageModel *model = [[MessageModel alloc]initWithDataDic:dic];
    NSLog(@"%@",model.name);
    NSLog(@"%@",model.address.city);
    NSLog(@"%@",((MessageLinkModel *)model.links[0]).name);

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
